package xyz.example.jdbc;

import lombok.SneakyThrows;
import org.junit.Test;
import xyz.lwm.lazycat.ds.DatasourceProperties;
import xyz.lwm.lazycat.ds.DatasourceProvider;
import xyz.lwm.lazycat.jdbc.exec.JdbcExecution;
import xyz.lwm.lazycat.jdbc.exec.JdbcTemplate;
import xyz.lwm.lazycat.setting.Setting;
import xyz.lwm.lazycat.utility.reflect.ClassUtil;
import xyz.lwm.lazycat.utility.reflect.ReflectUtil;

import javax.sql.DataSource;
import java.util.List;

public class JdbcTest {

    @Test
    @SneakyThrows
    public void test() {
        DataSource dataSource = dataSource();
        JdbcExecution je = new JdbcTemplate(dataSource);
        List<Student> students = je.queryList("select * from student", Student.class);
        students.forEach(System.out::println);
    }


    @SneakyThrows
    private DataSource dataSource() {
        DatasourceProperties dsProps = Setting.get().forPath(DatasourceProperties.class);
        String datasourceProvider = dsProps.getDatasourceProvider();
        Class<?> clz = ClassUtil.getDefaultClassLoader().loadClass(datasourceProvider);
        DatasourceProvider provider = (DatasourceProvider) ReflectUtil.newInstance(clz);
        return provider.getDatasource();
    }

}
